package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.SessionUtil;
import com.example.demo.pojo.Draft;
import com.example.demo.pojo.User;
import com.example.demo.service.ArticleService;
import com.example.demo.service.DraftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-02-21
 * Time: 10:35
 */
@RestController
@RequestMapping("/draft")
public class DraftController {

    @Autowired
    DraftService draftService;

    /**
     * 保存草稿接口
     * @param request
     * @param title
     * @param content
     * @return
     */
    @RequestMapping("/save")
    public Object saveDraft(HttpServletRequest request, String title, String content){
        if( !StringUtils.hasLength(title) || !StringUtils.hasLength(content)){
            return AjaxResult.fail(403,"参数不全");
        }
        //获取当前登录用户的id
        Integer uid = SessionUtil.getUserInfo(request).getId();
        if(uid!=null) {
            return draftService.saveDraft(uid, title, content);
        }
        return AjaxResult.fail(403,"用户未登录");
    }

    /**
     * 获取草稿列表
     * @param request
     * @return
     */
    @RequestMapping("/list")
    public Object listDraft(HttpServletRequest request){
        User userInfo = SessionUtil.getUserInfo(request);
        if( userInfo!=null){
            return draftService.getDraftList(userInfo.getId());
        }
        return null;
    }

    /**
     * 查看草稿详情
     * @param
     * @return
     */
    @RequestMapping("/detail")
    public Object getArticleDetails(Integer aid){
        if( aid!=null && aid >0){
            return AjaxResult.success(draftService.detailDraft(aid));
        }
        return AjaxResult.fail(-1,"查询失败");

    }

    /**
     * 修改文章接口
     * @param aid 文章id
     * @param title 文章标题
     * @param content 文章内容
     * @param request 请求
     * @return
     */
    @RequestMapping("/update")
    public Object updateArticle( Integer aid, String title, String content, HttpServletRequest request){
        if( aid!=null && aid>0){
            User userInfo = SessionUtil.getUserInfo(request);
            if( userInfo!=null){
                return draftService.updateDraft(aid, userInfo.getId(), title, content);
            }
        }
        return AjaxResult.fail(401, "修改失败");
    }

    /**
     * 删除草稿文章接口
     * @param aid
     * @param request
     * @return
     */
    @RequestMapping("/delete")
    public Object daleteArticle( Integer aid, HttpServletRequest request){
        if( aid!=null && aid>0){
            User userInfo = SessionUtil.getUserInfo(request);
            if( userInfo!=null){
                return draftService.deleteDraft(aid);
            }
        }
        return AjaxResult.fail(401,"请先登录");
    }
}
